<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Model\Admin\Admin;
use Session;
class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $data = DB::table('admin')->get();
        $data1 = Session::get('admin')[0];
       // dd($data1);
        return view('admin.admin_list',['admin'=>$data,'adm'=>$data1]);
       
       
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.admin_create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        dd($request);
         $data['name']     = $request->adminName;
        $data['nickname'] = $request->nickName;
        $data['password'] = md5($request->password);
        $data['phone']    = $request->phone;
        $data['email']    = $request->email;
        $data['count']    = 1;
        $data['lev']      = $request->adminRole;
        $data['time']     = time();
        DB::table('admin')->insert($data);
        return redirect('/admin/admin');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        return view('admin.admin_edit',['id'=>$id]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        
        $data['name']     = $request->name;
        $data['nickname'] = $request->nickname;
        $data['password'] = md5($request->name);
        $data['phone']    = $request->phone;
        $data['email']    = $request->email;
        $data['lev']      = $request->lev;
        $id = DB::table('admin')->where('id',$id)->update($data);
        if($id){
            return response()->json(['status' => '200']);
        }
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    //删除管理员
    public function del($id)
    {
       

        DB::table('admin')->where('id',$id)->delete();
        return response()->json(['status' => '200']);
    }
    //添加管理员
     public function add(Request $request)
    {
        //dd($request->adminName);
        $data['name']     = $request->adminName;
        $data['nickname'] = $request->nickName;
        $data['password'] = md5($request->password);
        $data['phone']    = $request->phone;
        $data['email']    = $request->email;
        $data['count']    = 1;
        $data['lev']      = $request->adminRole;
        $data['time']     = time();
        DB::table('admin')->insert($data);
        return redirect('/admin/admin');
    }
}
